import random
import clickhouse_connect
from function_tool import time_format_transform, function_help
import uuid
import datetime


class srds_generate_data(object):
    fun_tool = function_help.Function_Help()
    ck_connect_info = {
        'port': 8123,
        'host': '192.168.104.243',
        'username': 'default',
        'password': 'bwda2025',
        'database': 'lap'
    }
    client = clickhouse_connect.get_client(**ck_connect_info)
    random_time = time_format_transform.TimeFormatTransform()
    mysql_connection = {
        'port': 28002,
        'host': '192.168.2.210',
        'user': 'root',
        'password': 'cI5v4N2Q%a',
        'database': 'lap'
    }

    def dr_31001(self, num, start_date, end_date):
        sql00 = """
        select user_code from dr_32001_middle where lock_state = '0'
        """
        result00 = self.fun_tool.conn_mysql(sql00, self.mysql_connection)
        sql01 = """
        select resource_account_name from dr_32002_middle where lock_state = '0'
        """
        result01 = self.fun_tool.conn_mysql(sql01, self.mysql_connection)
        sql02 = """
        select resource_code from dr_33001_middle
        """
        result02 = self.fun_tool.conn_mysql(sql02, self.mysql_connection)
        table_name = 'dwd_id_app_logs_local'
        column_list = ['zy_log_id', 'zy_app_session_id', 'zy_resource_name', 'zy_resource_code',
                       'zy_slave_account_name', 'zy_master_account_name',
                       'zy_op_time', 'zy_op_result', 'zy_client_ip', 'zy_service_ip', 'zy_service_port',
                       'zy_module_code',
                       'zy_module_name', 'zy_op_type_code', 'zy_op_type_name', 'zy_op_content', 'zy_gold_apply_id']
        zy_op_result_list = ['1', '0']
        zy_service_port_list = ['3306', '28002', '9092', '8123', '6379']
        insert_data = []
        for i in range(num):
            zy_log_id = str(random.randint(10 ** 12, 10 ** 14))
            zy_app_session_id = uuid.uuid4().hex
            zy_resource_name = random.choice(result01)['resource_account_name']
            zy_resource_code = random.choice(result02)['resource_code']
            zy_slave_account_name = random.choice(result01)['resource_account_name']
            zy_master_account_name = random.choice(result00)['user_code']
            zy_op_time = self.random_time.get_random_date(start_date, end_date)
            zy_op_result = random.choice(zy_op_result_list)
            zy_client_ip = '.'.join(['192', '168', str(random.randint(1, 7)), str(random.randint(1, 255))])
            zy_service_ip = '.'.join(['192', '168', str(random.randint(1, 7)), str(random.randint(1, 255))])
            zy_service_port = random.choice(zy_service_port_list)
            zy_module_code = 'zy_module_code' + str(i).zfill(5)
            zy_module_name = 'zy_module_name' + str(i).zfill(5)
            zy_op_type_code = 'zy_op_type_code' + str(i).zfill(5)
            zy_op_type_name = 'zy_op_type_name' + str(i).zfill(5)
            zy_op_content = 'select * from dr_31001'
            zy_gold_apply_id = ''
            insert_data.append(
                [zy_log_id, zy_app_session_id, zy_resource_name, zy_resource_code, zy_slave_account_name,
                 zy_master_account_name,
                 zy_op_time, zy_op_result, zy_client_ip, zy_service_ip, zy_service_port,
                 zy_module_code,
                 zy_module_name, zy_op_type_code, zy_op_type_name, zy_op_content, zy_gold_apply_id])
        self.client.insert(table_name, insert_data, column_list)
        self.client.close()
        return 'success'

    def dr_31002(self, num, start_date, end_date):
        sql00 = """
        select url from dr_33004_middle
        """
        result00 = self.fun_tool.conn_mysql(sql00, self.mysql_connection)
        table_name = 'ods_id_sen_interface_logs_local'
        column_list = ['log_id', 'session_id', 'call_date', 'call_system_code',
                       'call_system_name', 'start_info', 'start_ip',
                       'called_system_code', 'called_system_name', 'interface_name', 'interface_ip', 'auth_result',
                       'result_code', 'param_pin', 'param_pout']
        auth_result_list = ['0', '1']
        result_code_list = ['0', '1']
        insert_data = []
        for i in range(num):
            log_id = str(random.randint(10 ** 12, 10 ** 14))
            session_id = uuid.uuid4().hex
            call_date = self.random_time.get_random_date_str(start_date, end_date)
            call_system_code = 'call_system_code' + str(i).zfill(5)
            call_system_name = 'call_system_name' + str(i).zfill(5)
            start_info = 'begin'
            start_ip = '.'.join(['192', '168', str(random.randint(1, 7)), str(random.randint(1, 255))])
            called_system_code = 'called_system_code' + str(i).zfill(5)
            called_system_name = 'called_system_name' + str(i).zfill(5)
            interface_name = random.choice(result00)['url']
            interface_ip = '.'.join(['192', '168', str(random.randint(1, 7)), str(random.randint(1, 255))])
            auth_result = random.choice(auth_result_list)
            result_code = random.choice(result_code_list)
            param_pin = 'sadjlkasjd|afsaf|dsagfasdf|1234|qfwqef|1231234'
            param_pout = '234t23g34|ewrth436|3h35|43r12rf13f|2134r21rfg'
            insert_data.append(
                [log_id, session_id, call_date, call_system_code,
                 call_system_name, start_info, start_ip
                    , called_system_code, called_system_name, interface_name, interface_ip, auth_result,
                 result_code, param_pin, param_pout])
        self.client.insert(table_name, insert_data, column_list)
        self.client.close()
        return 'success'

    def dr_31003(self, num, start_date, end_date):
        sql00 = """
                select user_code from dr_32001_middle where lock_state = '0'
                """
        result00 = self.fun_tool.conn_mysql(sql00, self.mysql_connection)
        sql01 = """
                select resource_account_name from dr_32002_middle where lock_state = '0'
                """
        result01 = self.fun_tool.conn_mysql(sql01, self.mysql_connection)
        sql02 = """
                select resource_code from dr_33001_middle
                """
        result02 = self.fun_tool.conn_mysql(sql02, self.mysql_connection)
        table_name = 'dwd_id_sys_logs_local'
        column_list = ['zy_log_id', 'zy_app_session_id', 'zy_resource_name', 'zy_resource_code',
                       'zy_slave_account_name', 'zy_master_account_name', 'zy_op_time', 'zy_op_result',
                       'zy_client_ip', 'zy_service_ip', 'zy_service_port', 'zy_op_obj',
                       'zy_op_content', 'zy_gold_apply_id', 'zy_data_code']
        zy_op_result_list = ['0', '1']
        zy_data_code_list = ['', '1']
        zy_gold_apply_id_list = ['', '1']
        zy_service_port_list = ['3306', '28002', '9092', '8123', '6379']
        insert_data = []
        for i in range(num):
            zy_log_id = str(random.randint(10 ** 12, 10 ** 14))
            zy_app_session_id = uuid.uuid4().hex
            zy_resource_name = random.choice(result01)['resource_account_name']
            zy_resource_code = random.choice(result02)['resource_code']
            zy_slave_account_name = random.choice(result01)['resource_account_name']
            zy_master_account_name = random.choice(result00)['user_code']
            zy_op_time = self.random_time.get_random_date(start_date, end_date)
            zy_op_result = random.choice(zy_op_result_list)
            zy_client_ip = '.'.join(['192', '168', str(random.randint(1, 7)), str(random.randint(1, 255))])
            zy_service_ip = '.'.join(['192', '168', str(random.randint(1, 7)), str(random.randint(1, 255))])
            zy_service_port = random.choice(zy_service_port_list)
            zy_op_obj = ''
            zy_op_content = 'select * from dr_31003'
            zy_gold_apply_id = random.choice(zy_gold_apply_id_list)
            zy_data_code = random.choice(zy_data_code_list)
            insert_data.append(
                [zy_log_id, zy_app_session_id, zy_resource_name, zy_resource_code,
                 zy_slave_account_name, zy_master_account_name, zy_op_time, zy_op_result,
                 zy_client_ip, zy_service_ip, zy_service_port, zy_op_obj,
                 zy_op_content, zy_gold_apply_id, zy_data_code])
        self.client.insert(table_name, insert_data, column_list)
        self.client.close()
        return 'success'

    def dr_31004(self, num, start_date, end_date):
        sql00 = """
                select user_code from dr_32001_middle where lock_state = '0'
                """
        result00 = self.fun_tool.conn_mysql(sql00, self.mysql_connection)
        table_name = 'dwd_id_gold_apply_logs'
        column_list = ['zy_id', 'zy_gold_apply_id', 'zy_event_type',
                       'zy_apply_master_account_name', 'zy_apply_time', 'zy_apply_content', 'zy_apply_resource_code',
                       'zy_gold_scene_id', 'zy_auth_trigger_mode', 'zy_auth_condition', 'zy_auth_start_time',
                       'zy_auth_end_time']
        zy_gold_scene_id_list = ['1', '2', '3', '4']
        zy_auth_trigger_mode_list = ['0', '1']
        zy_auth_condition_list = ['0', '1']
        insert_data = []
        for i in range(num):
            zy_id = str(random.randint(10 ** 12, 10 ** 14))
            zy_gold_apply_id = uuid.uuid4().hex
            zy_event_type = '0'
            zy_apply_master_account_name = random.choice(result00)['user_code']
            zy_apply_time = self.random_time.get_random_date(start_date, end_date)
            zy_apply_content = '搭建环境使用'
            zy_apply_resource_code = 'zy_apply_resource_code' + str(i).zfill(6)
            zy_gold_scene_id = random.choice(zy_gold_scene_id_list)
            zy_auth_trigger_mode = random.choice(zy_auth_trigger_mode_list)
            zy_auth_condition = random.choice(zy_auth_condition_list)
            zy_auth_start_time = zy_apply_time + datetime.timedelta(hours=1)
            zy_auth_end_time = zy_apply_time + datetime.timedelta(hours=2)
            insert_data.append(
                [zy_id, zy_gold_apply_id, zy_event_type,
                 zy_apply_master_account_name, zy_apply_time, zy_apply_content, zy_apply_resource_code,
                 zy_gold_scene_id, zy_auth_trigger_mode, zy_auth_condition, zy_auth_start_time,
                 zy_auth_end_time])
        self.client.insert(table_name, insert_data, column_list)
        self.client.close()
        return 'success'

    def dr_31005(self):
        sql00 = """
        select user_code from dr_32001_middle where lock_state = '0' and  zy_event_type = '0'
        """
        result00 = self.fun_tool.conn_mysql(sql00, self.mysql_connection)
        table_name = 'dwd_id_gold_apply_logs'
        column_list = ['zy_id', 'zy_gold_apply_id', 'zy_event_type',
                       'zy_coo_master_account_name', 'zy_approve_time', 'zy_apply_content', 'zy_auth_mode',
                       'kz_auth_gold_result', 'zy_approve_opinion']
        insert_data = []
        sql_ck = """
        select zy_gold_apply_id, zy_apply_time from dwd_id_gold_apply_logs
        """
        zy_auth_mode_list = ['0', '1']
        kz_auth_gold_result_list = ['0', '1']
        result_ck = self.client.query(sql_ck)
        result = result_ck.result_rows
        for i in range(50000):
            zy_id = str(random.randint(10 ** 12, 10 ** 14))
            zy_gold_apply_id = result[i][0]
            zy_event_type = '1'
            zy_coo_master_account_name = random.choice(result00)['user_code']
            zy_approve_time = result[i][1] + datetime.timedelta(seconds=10)
            zy_apply_content = '搭建环境使用'
            zy_auth_mode = random.choice(zy_auth_mode_list)
            kz_auth_gold_result = random.choice(kz_auth_gold_result_list)
            zy_approve_opinion = 'Y'
            insert_data.append(
                [zy_id, zy_gold_apply_id, zy_event_type,
                 zy_coo_master_account_name, zy_approve_time, zy_apply_content, zy_auth_mode,
                 kz_auth_gold_result, zy_approve_opinion])
        self.client.insert(table_name, insert_data, column_list)
        self.client.close()
        return 'success'

    def ck_dr_31001(self):
        column_list = ["log_id", "session_id", "resource_name", "resource_code", "slave_account_name",
                       "master_account_name", "op_time", "op_result", "client_ip", "server_ip", "port", "module_num",
                       "module_name", "op_type_code", "op_type_name", "op_content", "gold_apply_id", "bank_proof",
                       "data_is_sensitive", "sensitive_data_code", "authorized", "authorize_comment", "time_cycle",
                       "check_type", "result"]
        table_name = 'dr_31001_local'
        insert_data = []
        for i in range(500000):
            log_id = str(random.randint(10 ** 12, 10 ** 14))
            session_id = uuid.uuid4().hex
            resource_name = 'resource_name' + str(i).zfill(6)
            resource_code = 'resource_code' + str(i).zfill(6)
            slave_account_name = 'slave_account_name' + str(i).zfill(6)
            master_account_name = 'master_account_name' + str(i).zfill(6)
            op_time = '2025-09-26 00:00:00'
            op_result = random.choice(['0', '1'])
            client_ip = '192.168.2.121'
            service_ip = '192.168.103.49'
            service_port = str(random.choice(range(1000, 10000)))
            module_num = str(random.choice(range(1, 10)))
            module_name = 'module_name' + str(i).zfill(6)
            op_type_code = 'op_type_code' + str(i).zfill(6)
            op_type_name = 'op_type_name' + str(i).zfill(6)
            op_content = 'select * from t_sdc_audit_result'
            gold_apply_id = 'gold_apply_id' + str(i).zfill(6)
            bank_proof = random.choice(['0', '1'])
            data_is_sensitive = random.choice(['0', '1'])
            if data_is_sensitive == '1':
                sensitive_data_code = 'sensitive_data_code' + str(i).zfill(6)
            else:
                sensitive_data_code = ''
            authorized = random.choice(['0', '1'])
            if authorized == '1':
                authorize_comment = 'authorize_comment' + str(i).zfill(6)
            else:
                authorize_comment = ''
            time_cycle = '2025092500'
            check_type = '1'
            result = random.choice(['0', '1'])
            insert_data.append(
                [log_id, session_id, resource_name, resource_code, slave_account_name,
                 master_account_name, op_time, op_result, client_ip, service_ip, service_port, module_num,
                 module_name, op_type_code, op_type_name, op_content, gold_apply_id, bank_proof,
                 data_is_sensitive, sensitive_data_code, authorized, authorize_comment, time_cycle,
                 check_type, result])
        self.client.insert(table_name, insert_data, column_list)
        self.client.close()
        return 'success'

    def ck_dr_31002(self, time_cycle, num):
        column_list = ['old_log_id', 'session_id', 'call_date', 'call_system_code', 'call_system_name', 'start_info',
                       'start_ip', 'called_system_code', 'called_system_name', 'interface_name', 'interface_ip',
                       'auth_result', 'result_code', 'param_pin', 'param_pout', 'asset_code', 'time_cycle',
                       'check_type', 'result']
        table_name = 'dr_31002_local'
        insert_data = []
        for i in range(num):
            ord_log_id = str(random.randint(10 ** 12, 10 ** 14))
            session_id = uuid.uuid4().hex
            call_date = '20250928'
            call_system_code = 'call_system_code' + str(i).zfill(6)
            call_system_name = 'call_system_name' + str(i).zfill(6)
            start_info = 'start_info' + str(i).zfill(6)
            start_ip = '192.168.2.121'
            called_system_code = 'called_system_code' + str(i).zfill(6)
            called_system_name = 'called_system_name' + str(i).zfill(6)
            interface_name = 'login'
            interface_ip = '192.168.103.49'
            auth_result = random.choice(['0', '1'])
            result_code = auth_result
            param_pin = 'param_pin' + str(i).zfill(6)
            param_pout = 'param_pout' + str(i).zfill(6)
            asset_code = 'asset_code' + str(i).zfill(6)
            time_cycle = time_cycle
            check_type = '0'
            result = random.choice(['0', '1'])
            insert_data.append([ord_log_id, session_id, call_date, call_system_code, call_system_name, start_info,
                                start_ip, called_system_code, called_system_name, interface_name, interface_ip,
                                auth_result, result_code, param_pin, param_pout, asset_code, time_cycle, check_type,
                                result])
        self.client.insert(table_name, insert_data, column_list)
        self.client.close()
        return 'success'

    def ck_dr_31003(self, time_cycle, num):
        column_list = ['log_id', 'session_id', 'resource_name', 'resource_code', 'slave_account_name',
                       'master_account_name', 'op_time', 'op_result', 'client_ip', 'server_ip', 'port', 'op_object',
                       'op_cmd', 'treasury_apply_id', 'bank_proof', 'op_content', 'bath_opt', 'data_is_sensitive',
                       'sensitive_data_code', 'authorized', 'authorize_comment', 'time_cycle',
                       'check_type', 'result']
        table_name = 'dr_31003_local'
        insert_data = []
        for i in range(num):
            log_id = str(random.randint(10 ** 12, 10 ** 14))
            session_id = uuid.uuid4().hex
            resource_name = 'resource_name' + str(i).zfill(6)
            resource_code = 'resource_code' + str(i).zfill(6)
            slave_account_name = 'slave_account_name' + str(i).zfill(6)
            master_account_name = 'master_account_name' + str(i).zfill(6)
            op_time = '2025-09-26 00:00:00'
            op_result = random.choice(['0', '1'])
            client_ip = '192.168.2.121'
            server_ip = '192.168.103.49'
            port = str(random.choice(range(1000, 10000)))
            op_object = 'op_object' + str(i).zfill(6)
            op_cmd = 'mkdir'
            treasury_apply_id = 'treasury_apply_id' + str(i).zfill(6)
            bank_proof = random.choice(['0', '1'])
            op_content = 'select * from t_sdc_audit_detail'
            bath_opt = random.choice(['0', '1'])
            data_is_sensitive = random.choice(['0', '1'])
            if data_is_sensitive == '1':
                sensitive_data_code = 'sensitive_data_code' + str(i).zfill(6)
            else:
                sensitive_data_code = ''
            authorized = random.choice(['0', '1'])
            if authorized == '1':
                authorize_comment = 'authorize_comment' + str(i).zfill(6)
            else:
                authorize_comment = ''
            time_cycle = time_cycle
            check_type = '0'
            result = random.choice(['0', '1'])
            insert_data.append([log_id, session_id, resource_name, resource_code, slave_account_name,
                                master_account_name, op_time, op_result, client_ip, server_ip, port, op_object,
                                op_cmd, treasury_apply_id, bank_proof, op_content, bath_opt, data_is_sensitive,
                                sensitive_data_code, authorized, authorize_comment, time_cycle, check_type, result])
        self.client.insert(table_name, insert_data, column_list)
        self.client.close()
        return 'success'

    def ck_dr_31004(self, time_cycle, num):
        column_list = ['log_id', 'apply_name', 'op_time', 'apply_message', 'resource_code', 'scene_id', 'trigger_mode',
                       'authorization_condition', 'begin_time', 'end_time', 'treasury_apply_id', 'time_cycle',
                       'check_type', 'result']
        table_name = 'dr_31004_local'
        insert_data = []
        for i in range(num):
            log_id = str(random.randint(10 ** 12, 10 ** 14))
            apply_name = 'apply_name' + str(i).zfill(6)
            op_time = '2025-09-28 12:22:35'
            apply_message = 'apply_message' + str(i).zfill(6)
            resource_code = 'resource_code' + str(i).zfill(6)
            scene_id = str(random.choice(range(10 ** 4, 10 ** 6)))
            trigger_mode = random.choice(['0', '1'])
            authorization_condition = random.choice(['0', '1'])
            begin_time = '2025-09-28 00:00:00'
            end_time = '2025-09-28 23:59:59'
            treasury_apply_id = 'treasury_apply_id' + str(i).zfill(6)
            time_cycle = time_cycle
            check_type = '0'
            result = random.choice(['0', '1'])
            insert_data.append([log_id, apply_name, op_time, apply_message, resource_code, scene_id, trigger_mode,
                                authorization_condition, begin_time, end_time, treasury_apply_id, time_cycle,
                                check_type, result])
        self.client.insert(table_name, insert_data, column_list)
        self.client.close()
        return 'success'

    def ck_dr_31005(self, time_cycle, num):
        column_list = ['log_id', 'co_op_name', 'approve_time', 'treasury_apply_id', 'license_model', 'license_result',
                       'approval_opinion', 'time_cycle', 'check_type', 'result']
        table_name = 'dr_31005_local'
        insert_data = []
        for i in range(num):
            log_id = str(random.randint(10 ** 12, 10 ** 14))
            co_op_name = 'co_op_name' + str(i).zfill(6)
            approve_time = '2025-09-28 12:22:35'
            treasury_apply_id = 'treasury_apply_id' + str(i).zfill(6)
            license_model = random.choice(['0', '1'])
            license_result = random.choice(['0', '1'])
            approval_opinion = 'approval_opinion' + str(i).zfill(6)
            time_cycle = time_cycle
            check_type = '0'
            result = random.choice(['0', '1'])
            insert_data.append([log_id, co_op_name, approve_time, treasury_apply_id, license_model, license_result,
                                approval_opinion, time_cycle, check_type, result])
        self.client.insert(table_name, insert_data, column_list)
        self.client.close()
        return 'success'



if __name__ == '__main__':
    start_date = '2025-08-17'
    end_date = '2025-08-31'
    time_cycle = '20250928'
    # test = srds_generate_data().dr_31001(500000, start_date, end_date)
    test = srds_generate_data().ck_dr_31005(time_cycle, 200)
    print(test)
